package com.zhiche.lisa.bms.controller.capital;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrangement;
import com.zhiche.lisa.bms.pojo.vo.capital.CapitalArrangementAndDtl;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.service.capital.CapitalArrangementService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 排款计划 前端控制器
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-29
 */
@RestController
@RequestMapping("/capitalArrangement")
@Api(value = "/capitalArrangement", description = "排款计划接口", tags = {"排款计划接口"})
public class CapitalArrangementController {
    private static Logger LOGGER = LoggerFactory.getLogger(CapitalArrangementController.class);

    @Autowired
    private CapitalArrangementService capitalArrangementService;

    @PostMapping(value = "/selectCapitalArrangementPage")
    @ApiOperation(value = "排款计划列表", notes = "排款计划列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = CapitalArrangement.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<CapitalArrangement>> selectCapitalArrangementPage(@RequestBody Page<CapitalArrangement> inParam) {
        LOGGER.info("/CapitalArrangementController.selectCapitalArrangementPage Param {} ", inParam);
        RestfulResponse<Page<CapitalArrangement>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(capitalArrangementService.selectCapitalArrangement(inParam));
        } catch (BaseException e) {
            LOGGER.error("/CapitalArrangementController.selectCapitalArrangementPage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/CapitalArrangementController.selectCapitalArrangementPage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/selectCapitalArrangementAndDetail")
    @ApiOperation(value = "排款计划详明列表", notes = "排款计划详明列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = CapitalArrangement.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<CapitalArrangementAndDtl> selectCapitalArrangementAndDetail(@RequestBody CommonVO commonVO) {
        LOGGER.info("/CapitalArrangementController.selectCapitalArrangementAndDetail Param {} ", commonVO);
        RestfulResponse<CapitalArrangementAndDtl> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(capitalArrangementService.selectCapitalArrangementAndDetail(Long.valueOf(commonVO.getTypeVal())));
        } catch (BaseException e) {
            LOGGER.error("/CapitalArrangementController.selectCapitalArrangementAndDetail ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/CapitalArrangementController.selectCapitalArrangementAndDetail ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/insertCapitalArrangementAndDtl")
    @ApiOperation(value = "排款计划列表", notes = "排款计划列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = CapitalArrangement.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> insertCapitalArrangementAndDtl(@RequestBody CapitalArrangementAndDtl capitalArrangementAndDtl) {
        LOGGER.info("/CapitalArrangementController.insertCapitalArrangementAndDtl Param {} ", capitalArrangementAndDtl);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            capitalArrangementService.insertCapitalArrangementAndDtl(capitalArrangementAndDtl);
        } catch (BaseException e) {
            LOGGER.error("/CapitalArrangementController.insertCapitalArrangementAndDtl ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/CapitalArrangementController.insertCapitalArrangementAndDtl ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/capitalArrangementExamine")
    @ApiOperation(value = "排款计划审核", notes = "排款计划审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> capitalArrangementExamine(@RequestBody CommonVO commonVO, @RequestHeader("Authorization") String token) {
        LOGGER.info("CapitalArrangementController.capitalArrangementExamine Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            for (Long id : commonVO.getIds()) {
                try {
                    capitalArrangementService.capitalArrangementExamine(id, token);
                } catch (BaseException e) {
                    msg.add("审核排款计划:" + ":" + e.getMessage());
                } catch (Exception e) {
                    msg.add("审核排款计划:" + ":审核失败");
                }
            }
            if (!CollectionUtils.isEmpty(msg)) {
                restful.setData(msg);
                restful.setCode(-2);
            }
        } catch (BaseException e) {
            LOGGER.error("CapitalArrangementController.capitalArrangementExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CapitalArrangementController.capitalArrangementExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

}

